package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.ActivityPageDTO;
import com.sc.nft.dto.MallGoodsLabelQueryPageDTO;
import com.sc.nft.dto.MiniMallActivityPageDTO;
import com.sc.nft.dto.QueryMiniMallActivitySelectDTO;
import com.sc.nft.entity.MallActivity;
import com.sc.nft.entity.dto.ActivityCouponsProductDTO;
import com.sc.nft.entity.dto.ActivityShareProductDTO;
import com.sc.nft.entity.dto.PreferenceActivityDTO;
import com.sc.nft.entity.vo.mall.ActivityPageVO;
import com.sc.nft.entity.vo.mall.AdminSalesPageVO;
import com.sc.nft.entity.vo.mall.BargainPriceProductPageVO;
import com.sc.nft.entity.vo.mall.CategoryProductPageVO;
import com.sc.nft.entity.vo.mall.HotSaleActivityPageVO;
import com.sc.nft.enums.ApprovalStatusEnum;
import com.sc.nft.enums.ProductTypeEnum;
import com.sc.nft.enums.task.ActivityTypeEnum;
import com.sc.nft.mapper.MallActivityMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * IP商城营销活动表(sc_mall_activity)数据DAO
 *
 * @author zxy
 * @description IP商城
 * @since 2023-05-10 16:59:01
 */
@Slf4j
@Repository
public class MallActivityDao extends ServiceImpl<MallActivityMapper, MallActivity> {

    public MallActivity getByIdAndApprovalStatus(Long activityId, ApprovalStatusEnum approvalStatus) {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivity::getId, activityId);
        wrapper.eq(MallActivity::getApprovalStatus, approvalStatus);
        return getOne(wrapper);
    }

    public MallActivity getCanBuy(Long activityId) {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivity::getId, activityId);
        wrapper.eq(MallActivity::getApprovalStatus, ApprovalStatusEnum.PASS);
        wrapper.eq(MallActivity::getStatus, Boolean.TRUE);
        return getOne(wrapper);
    }

    public IPage<ActivityPageDTO> queryActivity(Long daoId, Boolean status, Boolean recommend, ApprovalStatusEnum approvalStatus, ProductTypeEnum productType, ActivityTypeEnum activityType, String productName, Boolean isMiniMall, Page toMp) {
        return baseMapper.queryActivity(toMp, daoId, status, recommend, approvalStatus != null ? approvalStatus.getCode() : null, productType != null ? productType.getCode() : null, activityType != null ? activityType.getCode() : null, productName, isMiniMall);
    }

    public IPage<ActivityPageVO> activityPage(Long daoId, Long categoryId, ActivityTypeEnum activityType, ProductTypeEnum type, String productName, Page page, Integer isShow, Boolean isMiniMall) {
        return baseMapper.activityPage(page, daoId, categoryId, activityType != null ? activityType.getCode() : null, type != null ? type.getCode() : null, productName, isShow, isMiniMall);
    }


    public List<Long> getThreeProductId() {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(MallActivity::getProductId);
        wrapper.eq(MallActivity::getStatus, true);
        wrapper.orderByDesc(MallActivity::getSort);
        wrapper.last("limit 3");
        return listObjs(wrapper, x -> (Long) x);
    }

    public List<Long> getIds(List<Long> ids) {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(MallActivity::getId);
        wrapper.in(MallActivity::getId, ids);
        wrapper.eq(MallActivity::getStatus, true);
        return listObjs(wrapper, x -> (Long) x);
    }

    public IPage<HotSaleActivityPageVO> hotSaleProductPage(String productName, Integer hotSaleSort, Page page) {
        return baseMapper.hotSaleProductPage(productName, hotSaleSort, page);
    }

    public IPage<BargainPriceProductPageVO> bargainPriceProductPage(String productName, Integer bargainPriceSort, Page page, Integer isShow) {
        return baseMapper.bargainPriceProductPage(page, productName, bargainPriceSort, isShow);
    }

    public IPage<CategoryProductPageVO> categoryProductPage(Page page, Long id, String productName, Integer sort, Boolean isVirtual, Integer isShow, Boolean showMiniMall) {
        return baseMapper.categoryProductPage(page, id, productName, sort, isVirtual, isShow, showMiniMall);
    }

    public IPage<AdminSalesPageVO> getSalesPage(Page page) {
        return baseMapper.getSalesPage(page);
    }

    public Page<ActivityCouponsProductDTO> getActivityCouponsProductDTOPage(Page page, List<Long> ids, Integer status) {
        return baseMapper.getActivityCouponsProductDTOPage(page, ids, status);
    }

    public List<ActivityCouponsProductDTO> getActivityCouponsProductDTOList(List<Long> ids) {
        return baseMapper.getActivityCouponsProductDTOPage(ids, null);
    }

    /**
     * 获取可添加至优惠券下的商品列表
     *
     * @param page
     * @param productName
     * @return
     */
    public Page<ActivityCouponsProductDTO> getActivityCouponsCanUseProductDTOPage(Page page, String productName) {
        return baseMapper.getActivityCouponsCanUseProductDTOPage(page, productName);
    }

    /**
     * 获取可添加至分享购下的商品列表
     *
     * @param page
     * @param productName
     * @return
     */
    public Page<ActivityShareProductDTO> getActivityShareCanUseProductDTOPage(Page page, String productName, Long daoId) {
        return baseMapper.getActivityShareCanUseProductDTOPage(page, productName, daoId);
    }

    public boolean checkProductActivity(Long productId) {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivity::getProductId, productId);
        wrapper.eq(MallActivity::getStatus, true);
        return count(wrapper) > 0;
    }

    public List<MallActivity> queryActivityByProductId(Long productId) {
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivity::getProductId, productId);
        return list(wrapper);
    }

    public IPage<ActivityPageVO> allCategories(Long daoId, Long categoryId, ActivityTypeEnum activityType, ProductTypeEnum productType, String productName, Page mp, Integer sort, Boolean isMiniMall) {
        return baseMapper.allCategories(mp, daoId, categoryId, activityType != null ? activityType.getCode() : null, productType != null ? productType.getCode() : null, productName, sort, isMiniMall);
    }

    /**
     * 修改活动展示渠道
     *
     * @param id
     * @param showChannel
     * @return
     */
    public boolean updateShowChannel(Long id, String showChannel, Boolean showMiniMall) {
        LambdaUpdateWrapper<MallActivity> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(MallActivity::getId, id);
        wrapper.set(MallActivity::getShowChannel, showChannel);
        wrapper.set(MallActivity::getShowMiniMall, showMiniMall);
        return update(new MallActivity(), wrapper);
    }

    public boolean updateDao(Long productId, Long daoId) {
        UpdateWrapper<MallActivity> wrapper = new UpdateWrapper<>();
        wrapper.eq("product_id", productId);
        wrapper.set("dao_id", daoId);
        return update(new MallActivity(), wrapper);
    }

    public Page<PreferenceActivityDTO> preferenceProductPage(Page page) {
        //好物优选
        page.setMaxLimit(20L);
        return baseMapper.preferenceProductPage(page);
    }

    public IPage<AdminSalesPageVO> getProductPage(Page mp) {
        return baseMapper.getProductPage(mp);
    }

    public IPage<MiniMallActivityPageDTO> queryActivityByMiniMall(Long daoId, Boolean status, Boolean recommend, ApprovalStatusEnum approvalStatus, String productName, Boolean showMiniMall, Page mp, Long templateId) {
        return baseMapper.queryActivityByMiniMall(mp, daoId, status, recommend, approvalStatus != null ? approvalStatus.getCode() : null, productName, showMiniMall, templateId);
    }

    public List<QueryMiniMallActivitySelectDTO> QueryMiniMallActivitySelect(Long id, String productName) {
        if (id == null && StringUtils.isEmpty(productName)) {
            return new ArrayList<>();
        }
        return baseMapper.QueryMiniMallActivitySelect(id, productName);
    }

    public IPage<MallGoodsLabelQueryPageDTO> queryByMallLabel(String productName, Long categoryId, Date startTime, Date endTime, Page mp) {
        return baseMapper.queryByMallLabel(productName, categoryId, startTime, endTime, mp);
    }

    public List<MallActivity> queryFinishedActivities() {
        LocalDateTime now = LocalDateTime.now();
        LambdaQueryWrapper<MallActivity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MallActivity::getStatus, true);
        wrapper.le(MallActivity::getEndTime, now);
        return list(wrapper);
    }

}